Hey all, greeting from Singapore 16M. I'm just started reading the tutorials today and I'm really interested to C++.
I don't understand Boolean operators, 'NOT', 'AND' and 'OR'.
Can someone help me out this part ?
Thanks,
hackerkts
Hey all, greeting from Singapore 16M. I'm just started reading the tutorials today and I'm really interested to C++.
I don't understand Boolean operators, 'NOT', 'AND' and 'OR'.
Can someone help me out this part ?
Thanks,
hackerkts
Post your code, comment on any confusion or compiler error(s)/warning(s). Visit the FAQ.
Last edited by Dave_Sinkula; 02-11-2006 at 10:20 PM. Reason: Backfilled a link to the FAQ.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Think of the logical operators just as you would with the same words in the english language.
Thanks for the reply, I don't the whole part for Boolean, how do I know the result is 0 or not ?
Just like the example given in the tutorial
Thanks,Code:A. !( 1 || 0 ) ANSWER: 0 B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR) C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
hackerkts
Here's a simple truth table:
0 && 0 = 0
0 && 1 = 0
1 && 0 = 0
1 && 1 = 1
0 || 0 = 0
0 || 1 = 1
1 || 0 = 1
1 || 1 = 1
!0 = 1
!1 = 0
>.< Do you mind giving me more details ? Sorry, this my 1st time learning on C++ need more times to understand basic stuff. Eh I mean the example you given can you indicate what does those means ?
Hope I didn't get the meaning wrong.0 && 0 = 0 //0 and 0 equal FALSE
Edit : Hey I think I got what it means, thanks man for your simple table. *Jumping with joy*
Thanks,
hackerkts
Last edited by hackerkts; 02-11-2006 at 10:58 PM.
In C++, 0 evaluates to false, and anything non-zero, like 1, evaluates to true.
The AND operator says that if any part (the left value or right value) evaluates to false, then the whole statement must be false. The OR operator says that if any part (the left value or right value) evaluates to true, then the whole statement must be true.
Let's put it this way:
Say your mom gives you two chores to do... "Rake the leaves AND mow the lawn." If you only rake the leaves, you still haven't done your chores. If you only mow the lawn, you still haven't done your chores. You must do both of them in order to complete your chores. Now lets say she was giving you an option... "Clean the counters OR mop the floor." If you Clean the counters, you've done your chore. If you mop the floor you've done your chore. If you do both, you've still done your chore and then some. If you do neither, obviously you haven't done your chore.
The NOT operator simply just flips the truth of the statement around. NOT TRUE equals FALSE and NOT FALSE equals TRUE and NOT (TRUE && TRUE || FALSE && TRUE) equals FALSE because the interior evaluates TRUE.
The order of operations as far as these go are in the order: NOT, AND, OR. If you take that last statement I wrote, NOT (TRUE && TRUE || FALSE && TRUE), and remove the parenthesis, NOT TRUE && TRUE || FALSE && TRUE, it should properly be read as this:
(NOT TRUE) && TRUE || FALSE && TRUE
or
FALSE && TRUE || FALSE && TRUE
or
FALSE || FALSE
equals
FALSE
Any questions?
Last edited by SlyMaelstrom; 02-12-2006 at 01:27 AM.
Sent from my iPadŽ
Nope, thank you sir !
That's a nice example too